/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.srwl.mytx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.srwl.mytx.R;
import com.srwl.mytx.fragment.HottestTopicFragment;
import com.srwl.mytx.fragment.NearbyTopicFragment;

import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;


public class TopicListActivity extends BaseActivity implements View.OnClickListener {
    private ViewPager vp_container;
    private TabLayout tab_bar;
    private ProgressBar progressBar;
    private HottestTopicFragment hottestTopicFragment;
    private NearbyTopicFragment nearbyTopicFragment;
    private ImageView iv_release;
    private RelativeLayout rl_search;
    private View view;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topic_list_layout);
        tab_bar = findViewById(R.id.tab_bar);
        progressBar = findViewById(R.id.progress_bar);
        iv_release = findViewById(R.id.iv_release);
        iv_release.setOnClickListener(this);
        rl_search = findViewById(R.id.rl_search);
        // rl_topBar = view.findViewById(R.id.rl_top_bar);
        rl_search.setOnClickListener(this);
        initViewPage();
    }

    private void initViewPage() {
        vp_container = findViewById(R.id.vp_container);
        vp_container.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_bar));
        tab_bar.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(vp_container));
        hottestTopicFragment = HottestTopicFragment.newInstance();
        hottestTopicFragment.setListener(isShowProgressBar -> {

            if (isShowProgressBar && !progressBar.isShown()) {
                progressBar.setVisibility(View.VISIBLE);
            } else if (!isShowProgressBar && progressBar.isShown()) {
                progressBar.setVisibility(View.GONE);
            }
        });
        nearbyTopicFragment = NearbyTopicFragment.newInstance();
        vp_container.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                switch (position) {
                    case 0:
                        return hottestTopicFragment;
                    case 1:
                        return nearbyTopicFragment;

                    default:
                        return null;
                }
            }

            @Override
            public int getCount() {
                return 2;
            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_release:
//                if (Utils.isTempUser(UserProfileManager.getInstance().getLoginUserInfo())) {
//                    startActivity(new Intent(TopicListActivity.this, LoginActivity.class).putExtra("EXTRA_CONTENT", "发布信息要先登录"));
//                    return;
//                }
//                String phoneNumber = UserProfileManager.getInstance().getUserPhoneNumber();
//                if (TextUtils.isEmpty(phoneNumber)) {
//                    ConfirmDialog confirmDialog = new ConfirmDialog(TopicListActivity.this, "提示", "绑定手机号才可以发布主题，去绑定？", true);
//                    confirmDialog.setConfirmListener(new ConfirmDialog.ConfirmListener() {
//                        @Override
//                        public void confirm() {
//                            startActivity(new Intent(TopicListActivity.this, UpdateUserInfoActivity.class));
//                        }
//                    });
//                    confirmDialog.show();
//                } else {
//                    startActivity(new Intent(TopicListActivity.this, PublishTopicActivity.class));
//                }
                startActivity(new Intent(TopicListActivity.this, PublishTopicActivity.class));
                break;
            case R.id.rl_search:
                startActivity(new Intent(TopicListActivity.this, TopicSearchActivity.class));
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Jzvd.goOnPlayOnResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        Jzvd.goOnPlayOnPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        JzvdStd.resetAllVideos();
    }

}
